package com.pucheng.bracelet.app.util

import android.text.TextUtils
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.pucheng.bracelet.app.ext.*
import com.pucheng.bracelet.data.bean.*
import com.pucheng.bracelet.viewmodel.meaasge.Push
import com.tencent.mmkv.MMKV
import com.zaaach.citypicker.model.City
import com.zaaach.citypicker.model.LocatedCity

object CacheUtil {

    /**
     * 获取保存的手表信息
     */
    fun getWatchInfo(): WatchInfoBean? {
        val kv = MMKV.mmkvWithID("app")
        val watchInfoStr = kv.decodeString(KEY_WATCH_INFO)
//        LogUtils.e("watchInfoStr = $watchInfoStr")
        return if (TextUtils.isEmpty(watchInfoStr)) {
            null
        } else {
            Gson().fromJson(watchInfoStr, WatchInfoBean::class.java)
        }
    }

    /**
     * 设置手表信息
     */
    fun setWatchInfo(watchInfoBean: WatchInfoBean?) {
        val kv = MMKV.mmkvWithID("app")
        if (watchInfoBean == null) {
            kv.encode(KEY_WATCH_INFO, "")
            setIsLogin(false)
        } else {
            kv.encode(KEY_WATCH_INFO, Gson().toJson(watchInfoBean))
            setIsLogin(true)
        }

    }

    /**
     * 是否已经登录
     */
    fun isLogin(): Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeBool("login", false)
    }

    /**
     * 设置是否已经登录
     */
    fun setIsLogin(isLogin: Boolean) {
        val kv = MMKV.mmkvWithID("app")
        kv.encode("login", isLogin)
    }

    /**
     * 是否是第一次登陆
     */
    fun isFirst(): Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeBool("first", true)
    }

    /**
     * 是否是第一次登陆
     */
    fun setFirst(first: Boolean): Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.encode("first", first)
    }

    /**
     * 首页是否开启获取指定文章
     */
    fun isNeedTop(): Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeBool("top", true)
    }

    /**
     * 设置首页是否开启获取指定文章
     */
    fun setIsNeedTop(isNeedTop: Boolean): Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.encode("top", isNeedTop)
    }

    /**
     * 获取搜索历史缓存数据
     */
    fun getSearchHistoryData(): ArrayList<String> {
        val kv = MMKV.mmkvWithID("cache")
        val searchCacheStr = kv.decodeString("history")
        if (!TextUtils.isEmpty(searchCacheStr)) {
            return Gson().fromJson(
                searchCacheStr
                , object : TypeToken<ArrayList<String>>() {}.type
            )
        }
        return arrayListOf()
    }

    fun setSearchHistoryData(searchResponseStr: String) {
        val kv = MMKV.mmkvWithID("cache")
        kv.encode("history", searchResponseStr)
    }

    /**
     * 设置步数目标
     */
    fun setStepsFlag(data: String) {
        val kv = MMKV.mmkvWithID("app")
        kv.encode(KEY_STEPS_FLAG, data)
    }

    /**
     * 获取步数目标
     */
    fun getStepsFlag(): String {
        val kv = MMKV.mmkvWithID("app")
        val stepsFlag = kv.decodeString(KEY_STEPS_FLAG)
        return if (TextUtils.isEmpty(stepsFlag)) {
            "8000"
        } else {
            stepsFlag
        }
    }

    /**
     * 设置全天心率检测是否打开
     */
    fun setOpenHRMeasure(isOpen: Boolean) {
        val kv = MMKV.mmkvWithID("app")
        kv.encode(KEY_OPEN_ALL_DAY_HR_MEASURE, isOpen)
    }

    /**
     * 获取全天心率检测是否打开
     */
    fun isOpenHRMeasure(): Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeBool(KEY_OPEN_ALL_DAY_HR_MEASURE)
    }

    /**
     * 设置翻腕亮屏是否打开
     */
    fun setOpenQuickView(isOpen: Boolean) {
        val kv = MMKV.mmkvWithID("app")
        kv.encode(KEY_QUICK_VIEW, isOpen)
    }

    /**
     * 获取翻腕亮屏是否打开
     */
    fun getQuickView(): Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeBool(KEY_QUICK_VIEW)
    }

    /**
     * 设置勿扰模式时段
     */
    fun setDoNotDisturb(data: String) {
        val kv = MMKV.mmkvWithID("app")
        kv.encode(KEY_DO_NOT_DISTURB, data)
    }

    /**
     * 获取勿扰模式时段
     */
    fun getDoNotDisturb(): String {
        val kv = MMKV.mmkvWithID("app")
        val quickViewPeriod = kv.decodeString(KEY_DO_NOT_DISTURB)
        return if (TextUtils.isEmpty(quickViewPeriod)) {
            "00:00-23:59"
        } else {
            quickViewPeriod
        }
    }

    /**
     * 设置翻腕亮屏生效时段
     */
    fun setQuickViewPeriod(data: String) {
        val kv = MMKV.mmkvWithID("app")
        kv.encode(KEY_QUICK_VIEW_PERIOD, data)
    }

    /**
     * 获取翻腕亮屏生效时段
     */
    fun getQuickViewPeriod(): String {
        val kv = MMKV.mmkvWithID("app")
        val quickViewPeriod = kv.decodeString(KEY_QUICK_VIEW_PERIOD)
        return if (TextUtils.isEmpty(quickViewPeriod)) {
            "00:00-23:59"
        } else {
            quickViewPeriod
        }
    }

    /**
     * 设置翻腕亮屏是否打开
     */
    fun setTimeSystem(is12Unit: Boolean) {
        val kv = MMKV.mmkvWithID("app")
        kv.encode(KEY_IS_12_UNIT, is12Unit)
    }

    /**
     * 获取当前时间格式是否为12小时制
     */
    fun getTimeSystem(): Boolean? {
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeBool(KEY_IS_12_UNIT, true)
    }

    /**
     * 设置久坐提醒
     */
    fun setOpenSitRemind(isOpen: Boolean) {
        val kv = MMKV.mmkvWithID("app")
        kv.encode(KEY_SIT_FOR_A_LONG, isOpen)
    }

    /**
     * 获取久坐提醒状态
     */
    fun isSitRemind(): Boolean? {
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeBool(KEY_SIT_FOR_A_LONG)
    }

    fun setSteps(stepsBean: StepsBean?) {
        val kv = MMKV.mmkvWithID("app")
        if (stepsBean == null) {
            kv.encode(KEY_STEPS, "")
        } else {
            kv.encode(KEY_STEPS, Gson().toJson(stepsBean))
        }
    }

    fun getSteps(): StepsBean? {
        val kv = MMKV.mmkvWithID("app")
        val stepsStr = kv.decodeString(KEY_STEPS)
//        LogUtils.e("watchInfoStr = $watchInfoStr")
        return if (TextUtils.isEmpty(stepsStr)) {
            StepsBean()
        } else {
            Gson().fromJson(stepsStr, StepsBean::class.java)
        }
    }

    fun setSao2Info(sao2Bean: Sao2Bean?) {
        val kv = MMKV.mmkvWithID("app")
        if (sao2Bean == null) {
            kv.encode(KEY_SAO2_INFO, "")
        } else {
            kv.encode(KEY_SAO2_INFO, Gson().toJson(sao2Bean))
        }
    }

    fun getSao2Info(): Sao2Bean? {
        val kv = MMKV.mmkvWithID("app")
        val watchInfoStr = kv.decodeString(KEY_SAO2_INFO)
//        LogUtils.e("watchInfoStr = $watchInfoStr")
        return if (TextUtils.isEmpty(watchInfoStr)) {
            Sao2Bean()
        } else {
            Gson().fromJson(watchInfoStr, Sao2Bean::class.java)
        }
    }

    fun setBloodPressureInfo(bloodPressureInfo: BloodPressureBean?) {
        val kv = MMKV.mmkvWithID("app")
        if (bloodPressureInfo == null) {
            kv.encode(KEY_BLOOD_PRESSURE_INFO, "")
        } else {
            kv.encode(KEY_BLOOD_PRESSURE_INFO, Gson().toJson(bloodPressureInfo))
        }
    }

    fun getBloodPressureInfo(): BloodPressureBean? {
        val kv = MMKV.mmkvWithID("app")
        val bloodPressureStr = kv.decodeString(KEY_BLOOD_PRESSURE_INFO)
//        LogUtils.e("watchInfoStr = $watchInfoStr")
        return if (TextUtils.isEmpty(bloodPressureStr)) {
            BloodPressureBean()
        } else {
            Gson().fromJson(bloodPressureStr, BloodPressureBean::class.java)
        }
    }

    /**
     * 缓存最后一次测量的结果
     */
    fun setHertRateInfo(bean: HertRateBean?) {
        val kv = MMKV.mmkvWithID("app")
        if (bean == null) {
            kv.encode(KEY_HERT_RATE_INFO, "")
        } else {
            kv.encode(KEY_HERT_RATE_INFO, Gson().toJson(bean))
        }
    }

    /**
     * 获取最后一次测量的结果
     */
    fun getHertRateInfo(): HertRateBean? {
        val kv = MMKV.mmkvWithID("app")
        val hertRateStr = kv.decodeString(KEY_HERT_RATE_INFO)
//        LogUtils.e("watchInfoStr = $watchInfoStr")
        return if (TextUtils.isEmpty(hertRateStr)) {
            HertRateBean()
        } else {
            Gson().fromJson(hertRateStr, HertRateBean::class.java)
        }
    }

    /**
     * 获取当前设置的闹钟重复周期
     */
    fun getWeekChecks(): Int {
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeInt(KEY_WEEK_CHECKS, 0)
    }

    /**
     * 设置当前设置的闹钟重复周期
     */
    fun setWeekChecks(week: Int) {
        val kv = MMKV.mmkvWithID("app")
        kv.encode(KEY_WEEK_CHECKS, week)
    }

    fun setUserInfo(bean: UserInfoBean?) {
        val kv = MMKV.mmkvWithID("app")
        if (bean == null) {
            kv.encode(KEY_USER_INFO, "")
        } else {
            kv.encode(KEY_USER_INFO, Gson().toJson(bean))
        }
    }

    /**
     * 获取个人信息
     */
    fun getUserInfo(): UserInfoBean {
        val kv = MMKV.mmkvWithID("app")
        val datStr = kv.decodeString(KEY_USER_INFO)
//        LogUtils.e("watchInfoStr = $watchInfoStr")
        return if (TextUtils.isEmpty(datStr)) {
            UserInfoBean()
        } else {
            Gson().fromJson(datStr, UserInfoBean::class.java)
        }
    }

    /**
     * 支持的功能
     */
    fun setSupportFunction(supportFunction: Int) {
        val kv = MMKV.mmkvWithID("app")
        kv.encode(KEY_SUPPORT_FUNCTION, supportFunction)
    }

    /**
     * 支持的功能,
     */
    fun getSupportFunction(): Int {
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeInt(KEY_SUPPORT_FUNCTION, 0xff)
    }

    /**
     * 设置消息推送的状态
     */
    fun setPushMsgState(bean: Push?) {
        val kv = MMKV.mmkvWithID("app")
        if (bean == null) {
            kv.encode(KEY_MSG_PUSH, "")
        } else {
            kv.encode(KEY_MSG_PUSH, Gson().toJson(bean))
        }
    }

    /**
     * 获取消息推送的状态
     */
    fun getPushMsgState(): Push {
        val kv = MMKV.mmkvWithID("app")
        val datStr = kv.decodeString(KEY_MSG_PUSH)
        return if (TextUtils.isEmpty(datStr)) {
            Push()
        } else {
            Gson().fromJson(datStr, Push::class.java)
        }
    }

    fun setCheckWeather(isOpen: Boolean) {
        val kv = MMKV.mmkvWithID("app")
        kv.encode(KEY_WEATHER, isOpen)
    }

    fun isCheckWeather():Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeBool(KEY_WEATHER, false)
    }

    /**
     * 设置选中的城市
     */
    fun setCityInfo(bean: City?) {
        val kv = MMKV.mmkvWithID("app")
        if (bean == null) {
            kv.encode(KEY_CITY, "")
        } else {
            kv.encode(KEY_CITY, Gson().toJson(bean))
        }
    }

    /**
     * 获取选中的城市
     */
    fun getCityInfo(): City {
        val kv = MMKV.mmkvWithID("app")
        val datStr = kv.decodeString(KEY_CITY)
        return if (TextUtils.isEmpty(datStr)) {
            City("深圳", "", "")
        } else {
            Gson().fromJson(datStr, City::class.java)
        }
    }
    /**
     * 设置选中的城市
     */
    fun setLocationCityInfo(bean: LocatedCity?) {
        val kv = MMKV.mmkvWithID("app")
        if (bean == null) {
            kv.encode(KEY_LOCATION_CITY, "")
        } else {
            kv.encode(KEY_LOCATION_CITY, Gson().toJson(bean))
        }
    }

    /**
     * 获取选中的城市
     */
    fun getLocationCityInfo(): LocatedCity {
        val kv = MMKV.mmkvWithID("app")
        val datStr = kv.decodeString(KEY_LOCATION_CITY)
        return if (TextUtils.isEmpty(datStr)) {
            LocatedCity("深圳", "中国", "22.572566", "113.865453","440306")
        } else {
            Gson().fromJson(datStr, LocatedCity::class.java)
        }
    }
}